package com.cgsd.heyu.ad;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;

import com.cgsd.heyu.R;

/**
 * @author xiaoxing
 * Description 开屏全拼dialog（用于fragment全屏显示开屏广告）
 * CreateData: 2020/11/3
 */
public class AdSplashDialog extends Dialog {

    public AdSplashDialog(@NonNull Context context, AdSplash adSplash) {
        super(context, R.style.dialog_full);
        setContentView(R.layout.dialog_ad_load);
        Window window = this.getWindow();
        if (window != null) {
            window.setGravity(Gravity.CENTER);
        }

        OnKeyListener keyListener = new DialogInterface.OnKeyListener(){
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode== KeyEvent.KEYCODE_BACK&&event.getRepeatCount()==0) {
                    return true;
                } else{
                    return false;
                }
            }
        };
        setOnKeyListener(keyListener);
        setCanceledOnTouchOutside(false);
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        // 获取屏幕宽、高用
        DisplayMetrics d = context.getResources().getDisplayMetrics();
        // 宽度设置为屏幕的0.7
        lp.width = (int) (d.widthPixels);
        lp.height = d.heightPixels;
        dialogWindow.setAttributes(lp);
        FrameLayout mFrameLayout = findViewById(R.id.mFrameLayout);
        adSplash.setFrameLayout(mFrameLayout,d.widthPixels,d.heightPixels);
    }

}
